﻿Imports System.Data.SqlClient

Public Class frmAddAccount

    Public CurrentLevel As Integer
    Public AccNo1, AccNo2, AccNo3, AccNo4 As String
    Public Acc1, Acc2, Acc3, Acc4, AccountName As String
    Public Saved As Boolean

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.ErrProv.Clear()
        Try
            If Me.txtAccNameEng.Text.Trim.Length = 0 Then
                Me.ErrProv.SetError(Me.txtAccNameEng, "Please enter account name and no.")
                Me.txtAccNameEng.Focus()

            ElseIf Me.txtAccNameAr.Text.Trim.Length = 0 Then
                Me.ErrProv.SetError(Me.txtAccNameAr, "Please enter account name and no.")
                Me.txtAccNameAr.Focus()

            Else
                Me.Cursor = Cursors.WaitCursor

                Dim Trans As SqlTransaction
                Dim cmd As New SqlCommand

                cnn.Open()
                cmd.Connection = cnn
                Trans = cnn.BeginTransaction
                cmd.Transaction = Trans

                'Open Account
                cmd.CommandText = "Insert Into Acc " & _
                                  "(AccNo,AccNo1,AccNo2,AccNo3,AccNo4,Acc1,Acc2,Acc3,Acc4) Values " & _
                                  "(@AccNo,@AccNo1,@AccNo2,@AccNo3,@AccNo4,@Acc1,@Acc2,@Acc3,@Acc4)"

                'Add values
                cmd.Parameters.AddWithValue("@AccNo", Me.txtAccNo1.Text.Trim + Me.txtAccNo2.Text.Trim + Me.txtAccNo3.Text.Trim + _
                                                      Me.txtAccNo4.Text.Trim)
                cmd.Parameters.AddWithValue("@AccNo1", Me.txtAccNo1.Text.Trim)
                cmd.Parameters.AddWithValue("@AccNo2", Me.txtAccNo2.Text.Trim)
                cmd.Parameters.AddWithValue("@AccNo3", Me.txtAccNo3.Text.Trim)
                cmd.Parameters.AddWithValue("@AccNo4", Me.txtAccNo4.Text.Trim)
                cmd.Parameters.AddWithValue("@Acc1", Acc1)
                cmd.Parameters.AddWithValue("@Acc2", Acc2)
                cmd.Parameters.AddWithValue("@Acc3", Acc3)
                cmd.Parameters.AddWithValue("@Acc4", Acc4)
                cmd.ExecuteNonQuery()

                'Set Translation
                cmd.CommandText = "Insert Into AccDictionary (AccNameEng,AccNameAr) Values (@AccNameEng,@AccNameAr)"

                cmd.Parameters.AddWithValue("@AccNameEng", Me.txtAccNameEng.Text.Trim)
                cmd.Parameters.AddWithValue("@AccNameAr", Me.txtAccNameAr.Text.Trim)
                cmd.ExecuteNonQuery()

                Trans.Commit()
                cnn.Close()

                AccountName = Me.txtAccNameEng.Text
                Saved = True

                Me.Cursor = Cursors.Default
                Me.Close()
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub FillNewAccNo()
        Try
            Me.Cursor = Cursors.WaitCursor

            If CurrentLevel = 0 Then
                FillLevel0()

            ElseIf CurrentLevel = 1 Then
                FillLevel1()

            ElseIf CurrentLevel = 2 Then
                FillLevel2()

            ElseIf CurrentLevel = 3 Then
                FillLevel3()
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillLevel0()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select IsNull(Max(AccNo1),0) From Acc", cnn)
            Dim SNo As String

            cnn.Open()
            SNo = (CInt(cmd.ExecuteScalar) + 1)
            cnn.Close()

            'Check if Maximum sub accounts is reached
            If CInt(SNo) > 9 Then
                Me.btnSave.Enabled = False
                Me.lblMsg.Text = "Maximum number of accounts reached"
                Exit Sub
            Else
                Me.lblMsg.Text = ""
            End If

            Me.txtAccNo1.Text = SNo
            Me.txtAccNo2.Text = "0"
            Me.txtAccNo3.Text = "00"
            Me.txtAccNo4.Text = "000000000"

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillLevel1()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select IsNull(Max(AccNo2),0) From Acc Where AccNo1=" & AccNo1, cnn)
            Dim SNo As String

            cnn.Open()
            SNo = (CInt(cmd.ExecuteScalar) + 1)
            cnn.Close()

            'Check if Maximum sub accounts is reached
            If CInt(SNo) > 9 Then
                Me.btnSave.Enabled = False
                Me.lblMsg.Text = "Maximum number of accounts reached"
                Exit Sub
            Else
                Me.lblMsg.Text = ""
            End If

            Me.txtAccNo1.Text = AccNo1
            Me.txtAccNo2.Text = SNo
            Me.txtAccNo3.Text = "00"
            Me.txtAccNo4.Text = "000000000"

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillLevel2()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select IsNull(Max(AccNo3),0) From Acc Where AccNo1=" & AccNo1 & _
                                      " and AccNo2=" & AccNo2, cnn)
            Dim SNo As String

            cnn.Open()
            SNo = (CInt(cmd.ExecuteScalar) + 1)
            cnn.Close()

            'Check if Maximum sub accounts is reached
            If CInt(SNo) > 99 Then
                Me.btnSave.Enabled = False
                Me.lblMsg.Text = "Maximum number of accounts reached"
                Exit Sub
            Else
                Me.lblMsg.Text = ""
            End If

            If SNo.Length = 1 Then
                SNo = "0" + SNo
            End If

            Me.txtAccNo1.Text = AccNo1
            Me.txtAccNo2.Text = AccNo2
            Me.txtAccNo3.Text = SNo
            Me.txtAccNo4.Text = "000000000"

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillLevel3()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select IsNull(Max(AccNo4),0) From Acc Where AccNo1=" & AccNo1 & _
                                      " and AccNo2=" & AccNo2 & " and AccNo3=" & AccNo3, cnn)
            Dim SNo As String

            cnn.Open()
            SNo = (CInt(cmd.ExecuteScalar) + 1)
            cnn.Close()

            'Check if Maximum sub accounts is reached
            If CInt(SNo) > 999999999 Then
                Me.btnSave.Enabled = False
                Me.lblMsg.Text = "Maximum number of accounts reached"
                Exit Sub
            Else
                Me.lblMsg.Text = ""
            End If

            If SNo.Length = 1 Then
                SNo = "00000000" + SNo

            ElseIf SNo.Length = 2 Then
                SNo = "0000000" + SNo

            ElseIf SNo.Length = 3 Then
                SNo = "000000" + SNo

            ElseIf SNo.Length = 4 Then
                SNo = "00000" + SNo

            ElseIf SNo.Length = 5 Then
                SNo = "0000" + SNo

            ElseIf SNo.Length = 6 Then
                SNo = "000" + SNo

            ElseIf SNo.Length = 7 Then
                SNo = "00" + SNo

            ElseIf SNo.Length = 8 Then
                SNo = "0" + SNo
            End If

            Me.txtAccNo1.Text = AccNo1
            Me.txtAccNo2.Text = AccNo2
            Me.txtAccNo3.Text = AccNo3
            Me.txtAccNo4.Text = SNo

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmAddAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Saved = False
    End Sub

    Private Sub txtAccName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAccNameEng.TextChanged
        If CurrentLevel = 0 Then
            Acc1 = Me.txtAccNameEng.Text.Trim
            Acc2 = ""
            Acc3 = ""
            Acc4 = ""

        ElseIf CurrentLevel = 1 Then
            Acc2 = Me.txtAccNameEng.Text.Trim
            Acc3 = ""
            Acc4 = ""

        ElseIf CurrentLevel = 2 Then
            Acc3 = Me.txtAccNameEng.Text.Trim
            Acc4 = ""

        ElseIf CurrentLevel = 3 Then
            Acc4 = Me.txtAccNameEng.Text.Trim
        End If
    End Sub
End Class